/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Images;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
//import sun.awt.image.PixelConverter;

/**
 *
 * @author Oho
 */
public class TImages extends Thread {

    Socket sk;
    Robot robot;
    Rectangle rt ;
    public TImages(Socket sk) {
        try {
            this.sk = sk;
            GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gDev = gEnv.getDefaultScreenDevice();

            //Get screen dimensions
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            //   rectangle = new Rectangle(dim);
             rt = new Rectangle(dim.width+50,dim.height+50);

            //Prepare Robot object
            robot = new Robot(gDev);
        } catch (AWTException ex) {
            Logger.getLogger(TImages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void run() {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(sk.getOutputStream());
            out.writeObject(rt);
            while (true) {
                BufferedImage bff = robot.createScreenCapture(rt);
                ImageIcon icon = new ImageIcon(bff);
                out.writeObject(icon);
                out.flush();
                out.reset();
                bff.flush();
                
                Thread.sleep(1500);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(TImages.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TImages.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(TImages.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
